home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13257 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: digex.net!not-for-mail
  2. From: brettb@cpcug.org (H Brett Bolen)
  3. Newsgroups: comp.arch.embedded,comp.lang.c
  4. Subject: Re: Using malloc of C on embedded system?
  5. Date: 5 Apr 1996 11:59:40 -0500
  6. Organization: Walrus Consulting, Germantown MD USA
  7. Message-ID: <4k3jhs$k5m@cpcug.org>
  8. References: <4jml7h$cbc@castor.usc.edu> <mark.stephens-0104961340270001@mstephens.gsfc.nasa.gov>
  9. NNTP-Posting-Host: cpcug.org
  10.  
  11.  
  12. In article <mark.stephens-0104961340270001@mstephens.gsfc.nasa.gov>,
  13. mark stephens <mark.stephens@gsfc.nasa.gov> wrote:
  14. >You also might consider having many "mallocs" for each type of data
  15. >structure you have.  I use a simple array of structures and a free list
  16. >composted of bytes.  When a structure is allocated, it's corresponding bit
  17. >is set in the free/used list.  Deallocation is the reverse.  Structures
  18. >are allocated by liner search of the free list.
  19. >
  20. >The restrictions are you have to know the maximum number of structures
  21. >used at any one time.  If you can know this, you will not run out of heap
  22. >and/or have the heap fractionated.   You run out of heap and your systems
  23. >runs out on you!
  24. >
  25. >mark
  26.  
  27. I've coded up a simular module.  Basically we wanted to dynamically
  28. allocate strucs, without using heap.  I coded it for my own use,
  29. but it's found it's way into a lot of code.  A major use is to 
  30. keep track of TCP clients in a real-time system.
  31.  
  32. My module's interface is something like this:
  33.  
  34. ------------------------------------------------------------ begin com_dtab.h
  35.  
  36. [...]
  37.  
  38. typedef struct {
  39.    unsigned int   used[4]; 
  40.    int            entry_size;
  41.    int            entry_last;
  42.    int            alloc_index;
  43.    int            iter_index;
  44.    void          *buf;
  45. }  DTAB_TABLE;
  46.  
  47.  
  48.  
  49. /*****
  50.  **  Dynamic Table 
  51.  *****/
  52. int   DTAB_create( DTAB_TABLE  *dtab, int ent_sz, int ent_max, void *table);
  53. void *DTAB_alloc(  DTAB_TABLE  *dtab);
  54. void  DTAB_free(   DTAB_TABLE  *dtab, void *entry);
  55. int   DTAB_used(   DTAB_TABLE  *dtab, void *entry);
  56. void *DTAB_first(  DTAB_TABLE  *dtab);
  57. void *DTAB_next(   DTAB_TABLE  *dtab);
  58. void  DTAB_print(  DTAB_TABLE  *dtab, char *banner);
  59.  
  60.  
  61. ------------------------------------------------------------ end com_dtab.h
  62.  
  63.  
  64. For each array to be allocated from, an extra struct ( DTAB for Dynamic
  65. table) is kept with the inuse flags.
  66.  
  67. One thing that made it easy to use is iteration using _first() and
  68. _next():
  69.  
  70.   it = DTAB_first();
  71.   while ( it)  {
  72.      process( it);
  73.      it = DTAB_next();
  74.   }
  75.  
  76.  
  77. Most functions are less than 5 lines of code. I think it had 62 SLOC --
  78. It shouldn't be too hard to re-engineer ( Although I lost a a few
  79. hair figuring out pointer subtraction).
  80.  
  81.  
  82. b\253              | Take Chances, Make Mistakes 
  83. brettb@cpcug.org   | Get Messy    
  84. brett bolen        |     - Ms Frizzle - MSB
  85. Walrus Consulting  | http://www.cpcug.org/user/brettb
  86.  
  87.  
  88. -- 
  89. b\253              | Take Chances, Make Mistakes 
  90. brettb@cpcug.org   | Get Messy    
  91. brett bolen        |     - Ms Frizzle - MSB
  92. Walrus Consulting  | http://www.cpcug.org/user/brettb
  93.